/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sergivonavi.materialbanner.app.slice;

import com.sergivonavi.materialbanner.app.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.agp.text.RichText;
import ohos.agp.text.RichTextBuilder;
import ohos.agp.text.TextForm;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;
import java.io.IOException;

public class AboutAbilitySlice extends AbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        try {
            int color = getResourceManager().getElement(ResourceTable.Color_title_bar_bg).getColor();
            getWindow().setStatusBarColor(color);
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_about);
        Component title = findComponentById(ResourceTable.Id_tx_title);
        if (title instanceof Text) {
            Text txTitle = (Text) title;
            txTitle.setFont(Font.DEFAULT_BOLD);
        }
        Component open = findComponentById(ResourceTable.Id_tx_open_source);
        if (open instanceof Text) {
            Text txOpenSource = (Text) open;
            txOpenSource.setFont(Font.DEFAULT_BOLD);
        }
        Component picasso = findComponentById(ResourceTable.Id_tx_picasso);
        if (picasso instanceof Text) {
            Text txPicasso = (Text) picasso;
            txPicasso.setFont(Font.DEFAULT_BOLD);
        }
        setDescription();
        setAddress();
        setLicense();
    }

    private void setDescription() {
        Component description = findComponentById(ResourceTable.Id_tx_description);
        if (description instanceof Text) {
            Text txDescription = (Text) description;
            RichText richText = new RichTextBuilder()
                    .mergeForm(new TextForm().setTextFont(Font.DEFAULT_BOLD).setTextSize(48))
                    .addText(getString(ResourceTable.String_app_name))
                    .revertForm().mergeForm(new TextForm().setTextSize(48))
                    .addText(getString(ResourceTable.String_library_MaterialBanner_libraryDescription))
                    .build();
            txDescription.setRichText(richText);
        }
    }

    private void setAddress() {
        Component address = findComponentById(ResourceTable.Id_tx_address);
        if (address instanceof Text) {
            Text txAddress = (Text) address;
            RichText richText = new RichTextBuilder()
                    .mergeForm(new TextForm().setUnderline(true).setTextSize(42)
                            .setTextColor(getColor(ResourceTable.Color_text_color2)))
                    .addText(getString(ResourceTable.String_library_MaterialBanner_libraryWebsite))
                    .build();
            txAddress.setRichText(richText);
            txAddress.setClickedListener(component -> {
                Intent intents = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withUri(Uri.parse(txAddress.getText()))
                        .withAction(IntentConstants.ACTION_SEARCH)
                        .build();
                intents.setOperation(operation);
                startAbility(intents, 0);
            });
        }
    }

    private void setLicense() {
        Component license1 = findComponentById(ResourceTable.Id_tx_license1);
        if (license1 instanceof Text) {
            Text txLicense1 = (Text) license1;
            txLicense1.setText(String.format("Copyright 2013 Square, Inc. %n %n" +
                    "Licensed under the Apache License, Version 2.0 (the \"License\");" +
                    "you may not use this file except in compliance with the License." +
                    "You may obtain a copy of the License at"));
        }
        Component license2 = findComponentById(ResourceTable.Id_tx_license2);
        if (license2 instanceof Text) {
            Text txLicense2 = (Text) license2;
            RichText richText = new RichTextBuilder()
                    .mergeForm(new TextForm().setUnderline(true).setTextSize(42)
                            .setTextColor(getColor(ResourceTable.Color_text_color2)))
                    .addText(getString(ResourceTable.String_picasso_license))
                    .build();
            txLicense2.setRichText(richText);
            txLicense2.setClickedListener(component -> {
                Intent intents = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withUri(Uri.parse(txLicense2.getText()))
                        .withAction(IntentConstants.ACTION_SEARCH)
                        .build();
                intents.setOperation(operation);
                startAbility(intents, 0);
            });
        }
        Component license3 = findComponentById(ResourceTable.Id_tx_license3);
        if (license3 instanceof Text) {
            Text txLicense3 = (Text) license3;
            txLicense3.setText(String.format("Unless required by applicable law or agreed to in writing, software " +
                    "distributed under the License is distributed on an \"AS IS\" BASIS, " +
                    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. " +
                    "See the License for the specific language governing permissions and " +
                    "limitations under the License."));
        }
    }
}
